﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.IO;
using SqlSugar;
using Newtonsoft.Json;
using System.Dynamic;
using ApiData.JsonDB;
using static ApiData.Common.EnumHelper;
using Microsoft.AspNetCore.Authorization;

namespace ApiData.Web.ApiControllers
{
    [AllowAnonymous]
    [ApiController]
    public class DataController : ControllerBase
    {
        private Common.ResultData result => new Common.ResultData();
        private readonly SqlSugarClient context;
        private readonly IMemoryCache cache;

        public DataController(SqlSugarClient context, IMemoryCache cache)
        {
            this.context = context;
            this.cache = cache;
        }

        [Route("Data/{usercode}/{appid}/{table}/{execute}/{value?}/{value2?}")]
        public Common.ResultData Index(string usercode, string appid, string table, string execute, string? value, string? value2)
        {
            var user = context.Queryable<Model.user>().First(x => x.code == usercode);
            if (user == null) return result.Fail("用户代码错误");
            var app = context.Queryable<Model.application>()
                .Includes(x => x.tables)
                .First(x => x.appid == appid && x.userid == user.id);
            if (app == null)
                return result.Fail("应用不存在");
            var tab = app.tables.FirstOrDefault(x => x.code == table);
            if (tab == null)
                return result.Fail("表不存在");

            var cols = tab.structure.Split("\r\n");
            List<Model.Dto.Structure> st = new List<Model.Dto.Structure>();
            foreach (var item in cols)
            {
                if (item.IsNullOrWhiteSpace()) break;
                var col = item.Split('-');
                st.Add(new Model.Dto.Structure()
                {
                    name = col[0],
                    type = col[1],
                    defaultvalue = col[2],
                    des = col[3]
                });
            }
            int index = 0;
            switch (execute.ToLower())
            {
                case "select":
                    return Logic.Select.Execute(tab, value, value2);
                case "like":
                    return Logic.Like.Execute(tab, value, value2);
                case "page":
                    return Logic.Page.Execute(tab, value, value2);
                case "struc":
                    var list = Logic.Select.Structure(tab);
                    return list;
                case "insert":
                    Dictionary<string, string> insertDic = new Dictionary<string, string>();
                    foreach (var item in st)
                        insertDic[item.name] = Request.Form[item.name];
                    return Logic.Insert.Execute(tab, insertDic);
                case "update":
                    index = Convert.ToInt32(value2);
                    Dictionary<string, string> updateDic = new Dictionary<string, string>();
                    foreach (var item in st)
                        updateDic[item.name] = Request.Form[item.name];
                    return Logic.Update.Execute(tab, updateDic, index);
                case "delete":
                    index = Convert.ToInt32(value2);
                    return Logic.Delete.Execute(tab, index);
                case "random":
                    return Logic.Randomx.Execute(tab, value);
                case "distinct":
                    return Logic.Distinct.Execute(tab, value);
                default:
                    return result.Fail("无此操作");
            }
        }
    }
}
